﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Maticsoft.Web.Admin.Feedback
{
    public partial class SurveyOptionList : PageBase
    {
        int _SurveyId;
        int _SurveyItemId;
        BLL.SurveyOption bll = new BLL.SurveyOption();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("feedback", "read");

            int.TryParse(Request.QueryString["surveyId"], out _SurveyId);
            int.TryParse(Request.QueryString["itemId"], out _SurveyItemId);
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            string strWhere = "IsActive=1";
            if (_SurveyId != 0)
            {
                strWhere += " and SurveyId=" + _SurveyId;
            }
            if (_SurveyItemId != 0)
            {
                strWhere += " and SurveyItemId=" + _SurveyItemId;
            }
            strWhere += " order by OrderNo asc";

            DataSet ds = bll.GetList(strWhere);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Check("feedback", "del");

            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                //bll.DeleteByIds(ids);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            editT.Visible = true;
            txtTitle.Text = "";
            txtOrderNo.Text = "";
            hidSurveyOptionId.Value = "";
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ModifyOption")
            {
                editT.Visible = true;
                Model.SurveyOption soModel = bll.GetModel(int.Parse(e.CommandArgument.ToString()));
                txtTitle.Text = soModel.SurveyOptionTitle;
                txtOrderNo.Text = soModel.OrderNo;
                hidSurveyOptionId.Value = soModel.SurveyOptionId.ToString();
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (hidSurveyOptionId.Value != "")
            {
                Model.SurveyOption soModel = bll.GetModel(int.Parse(hidSurveyOptionId.Value));
                soModel.SurveyOptionTitle = txtTitle.Text.Trim();
                soModel.OrderNo = txtOrderNo.Text.Trim();
                soModel.UpdateTime = DateTime.Now;
                bll.Update(soModel);
            }
            else
            {
                Model.SurveyOption soModel = new Model.SurveyOption();
                soModel.SurveyId = _SurveyId;
                soModel.SurveyItemId = _SurveyItemId;
                soModel.SurveyOptionTitle = txtTitle.Text.Trim();
                soModel.OrderNo = txtOrderNo.Text.Trim();
                soModel.CreateTime = DateTime.Now;
                soModel.UpdateTime = DateTime.Now;
                soModel.IsActive = 1;
                bll.Add(soModel);
            }

            BindData();
            editT.Visible = false;
        }


        protected void Button2_Click(object sender, EventArgs e)
        {
            editT.Visible = false;
            txtTitle.Text = "";
            txtOrderNo.Text = "";
            hidSurveyOptionId.Value = "";
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("SurveyItemList.aspx?SurveyId=" + _SurveyId);
        }
    }
}